在键盘上点击 done 会清除 flutter 中的文本
Hitting done on keyboard clears the text in flutter
我有一个 TextField()
和一个控制器,我将它传递给 TextField
控制器。我的问题是,每次我在键盘上点击完成时,它都会清除我的文本。
我关注了这个问题:Flutter keyboard done button causes textfield content to vanish
问题来解决我的问题,但对我来说没有任何效果。我一无所知,因此再次将这个问题放在 Whosebug 上。
我的代码:
class ReferralPage extends StatelessWidget {
final TextEditingController controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(
margin: EdgeInsets.only(top: 40.0, left: 16.0, right: 16.0),
child: Card(
color: Colors.white,
child: Padding(
padding: EdgeInsets.all(24.0),
child: TextField(
controller: controller,
cursorColor: Theme.of(context).primaryColor,
decoration: InputDecoration(hintText: 'Referral Code'),
)
)
),
decoration: BoxDecoration(boxShadow: [
new BoxShadow(color: Color.fromRGBO(173, 179, 191, 0.3), blurRadius: 20.0, offset: new Offset(0, 12))
])
)
);
}
}
如您所见,我的 TextEditingController()
不在 Widget build
内,因此没有发生这种情况的感觉。
The only solution I found to this problem was to remove the controller but I don't want to do this, I want to use my controller for other purposes. I don't know why this is happening.
如有任何帮助,我们将不胜感激。谢谢:)
您遇到此问题是因为您使用的是无状态小部件。将您的无状态小部件更改为有状态小部件,这样您的控制器就不会被重建。
我有一个 TextField()
和一个控制器,我将它传递给 TextField
控制器。我的问题是,每次我在键盘上点击完成时,它都会清除我的文本。
我关注了这个问题:Flutter keyboard done button causes textfield content to vanish 问题来解决我的问题,但对我来说没有任何效果。我一无所知,因此再次将这个问题放在 Whosebug 上。
我的代码:
class ReferralPage extends StatelessWidget {
final TextEditingController controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(
margin: EdgeInsets.only(top: 40.0, left: 16.0, right: 16.0),
child: Card(
color: Colors.white,
child: Padding(
padding: EdgeInsets.all(24.0),
child: TextField(
controller: controller,
cursorColor: Theme.of(context).primaryColor,
decoration: InputDecoration(hintText: 'Referral Code'),
)
)
),
decoration: BoxDecoration(boxShadow: [
new BoxShadow(color: Color.fromRGBO(173, 179, 191, 0.3), blurRadius: 20.0, offset: new Offset(0, 12))
])
)
);
}
}
如您所见,我的 TextEditingController()
不在 Widget build
内,因此没有发生这种情况的感觉。
The only solution I found to this problem was to remove the controller but I don't want to do this, I want to use my controller for other purposes. I don't know why this is happening.
如有任何帮助,我们将不胜感激。谢谢:)
您遇到此问题是因为您使用的是无状态小部件。将您的无状态小部件更改为有状态小部件,这样您的控制器就不会被重建。